package net.yxsoft.service.formdisigner;


import com.jfinal.plugin.activerecord.Db;
import net.yxsoft.kit.CustomDb;
import net.yxsoft.model.formdesigner.FormDataSource;
import net.yxsoft.resource.SqlRes;
import net.yxsoft.resource.TableRes;
import rapid.core.SqlList;
import rapid.core.TResult;
import rapid.jfinal.BaseService;
import rapid.jfinal.ParamKeyMap;
import rapid.util.StrKit;

import java.util.List;

/**
 * @author zhaohaibo
 */
public class FormDataSourceService extends BaseService {

    /**
    * @MethodName: find
    * @Description:
    * @param: params
    * @return * @return java.util.List<net.yxsoft.model.formdesigner.FormDataSource>
    * @throws
    * @date 2023/8/31 14:01
    * @author wj
    */
  public List<FormDataSource> find(ParamKeyMap params) {
    return FormDataSource.dao.find(Db.getSqlPara(SqlRes.FORM_DESIGNER + "findDataSource", params.toKv()));
  }
    /**
    * @MethodName: findText
    * @Description:
    * @param: pid
    * @return * @return java.util.List<java.lang.String>
    * @throws
    * @date 2023/8/31 14:01
    * @author wj
    */
  public List<String> findText(String pid) {
    return Db.query("select name from " + TableRes.FORM_DATASOURCE + " where pid = ? and state = 1", StrKit.nvl(pid, "0"));
  }

  /**
  * @MethodName: add
  * @Description: 
  * @param: obj
  * @return * @return rapid.core.TResult
  * @throws 
  * @date 2023/8/31 14:01
  * @author wj 
  */
  public TResult add(FormDataSource obj) {
    String pid = obj.getPid();
    String name = obj.getName();
    pid = pid == null ? "0" : pid;

    if(Db.findFirst("select id from " + TableRes.FORM_DATASOURCE + " where pid = ? and name = ?",
                    pid, obj.getName()) != null) {
      return fail().msg("已存在相同的名称：" + name + "。");
    }

    obj.setPid(pid);
    return obj.save() ? success() : fail();
  }

  /**
  * @MethodName: update
  * @Description: 
  * @param: obj
  * @return * @return rapid.core.TResult
  * @throws 
  * @date 2023/8/31 14:02
  * @author wj 
  */
  public TResult update(FormDataSource obj) {
    String id = obj.getId();
    String pid = obj.getPid();
    String name = obj.getName();
    pid = pid == null ? "0" : pid;

    if(Db.findFirst("select id from " + TableRes.FORM_DATASOURCE + " where id <> ? and pid = ? and name = ?", id, pid, name) != null) {
      return fail().msg("已存在相同的名称：" + name);
    }

    FormDataSource old = FormDataSource.dao.findById(id);

    if(old == null) {
      return fail().msg("记录不存在或已被删除。");
    }

    old.setPid(pid);
    old.setName(name);

    return old.update() ? success() : fail();
  }

  /**
  * @MethodName: toggleDisabled
  * @Description: 
  * @param: id
   * @param: state
  * @return * @return rapid.core.TResult
  * @throws 
  * @date 2023/8/31 14:02
  * @author wj 
  */
  public TResult toggleDisabled(String id, String state) {
    if(StrKit.isEmpty(id)) {
      return success();
    }

    CustomDb.updateWithExceptionHandling("update " + TableRes.FORM_DATASOURCE + " set state = ? where id = ?", state, id);

    return success();
  }

  /**
  * @MethodName: remove
  * @Description: 
  * @param: id
  * @return * @return rapid.core.TResult
  * @throws 
  * @date 2023/8/31 14:27
  * @author wj 
  */
  public TResult remove(String id) {
    if(StrKit.isEmpty(id)) {
      return success();
    }

    SqlList sqls = new SqlList();

    sqls.add("delete from %s where id = %s", TableRes.FORM_DATASOURCE, id)
      .add("delete from %s where pid = %s", TableRes.FORM_DATASOURCE, id);

    Db.batch(sqls, 10000);

    return success();
  }
}
